
/**
 * Created by admin on 16/5/3.
 *  机构树模糊查询
 *  
 */

function focusKey(e) {
        if (key.hasClass("empty")) {
            key.removeClass("empty");
        }
    }
function blurKey(e) {
    if (key.get(0).value === "") {
        key.addClass("empty");
    }
}
var lastValue = "", nodeList = [], fontCss = {};
function clickRadio(e) {
    lastValue = "";
    searchNode(e);
}
function searchNode(e) {
    //var zTree = $.fn.zTree.getZTreeObj("treeDemo");
    // if (!$("#getNodesByFilter").attr("checked")) {
    var value = $.trim(key.get(0).value);
    var keyType = "";
//            if ($("#name").attr("checked")) {
//                keyType = "name";
//            } else if ($("#level").attr("checked")) {
//                keyType = "level";
//                value = parseInt(value);
//            } else if ($("#id").attr("checked")) {
//                keyType = "id";
//                value = parseInt(value);
//            }

    // modify by wangjs==============按名称搜索
    keyType = "name";
    //================================

    if (key.hasClass("empty")) {
        value = "";
    }
    if (lastValue === value) return;
    lastValue = value;
    if (value === "") {
        zTree.showNodes(zTree.transformToArray(zTree.getNodes())) ;
        return;
    }

//            if ($("#getNodeByParam").attr("checked")) {
//                var node = zTree.getNodeByParam(keyType, value);
//                if (node === null) {
//                    nodeList = [];
//                } else {
//                    nodeList = [node];
//                }
//            } else if ($("#getNodesByParam").attr("checked")) {
//                nodeList = zTree.getNodesByParam(keyType, value);
//            } else if ($("#getNodesByParamFuzzy").attr("checked")) {
    nodeList = zTree.getNodesByParamFuzzy(keyType, value);
    // }
    //} else {
    //updateNodes(false);
    // nodeList = zTree.getNodesByFilter(filter);

    /**不查询父级
     for(var x = 0 ; x<nodeList.length ; x++){
                if(nodeList[x].isParent){
                    nodeList.splice(x--,1);
                }
            }
     */
    nodeList = zTree.transformToArray(nodeList);
    updateNodes(true);

}
function updateNodes(highlight) {
    //  var zTree = $.fn.zTree.getZTreeObj("treeDemo");
    var allNode = zTree.transformToArray(zTree.getNodes());
    zTree.hideNodes(allNode);
    for(var n in nodeList){
        findParent(zTree,nodeList[n]);
    }

    zTree.showNodes(nodeList);
}

function findParent(zTree,node){
    //  zTree.expandNode(node,true,false,false);
    var pNode = node.getParentNode();
    if(pNode != null){
        nodeList.push(pNode);
        findParent(zTree,pNode);
    }

}


function getFontCss(treeId, treeNode) {
    return (!!treeNode.highlight) ? {color:"#A60000", "font-weight":"bold"} : {color:"#333", "font-weight":"normal"};
}
function filter(node) {
    return !node.isParent && node.isFirstNode;
}
